#! /usr/bin/sdlbrt

finp$="picture.png"

'open "_xpm.xpm" for output as #1
'file output #1,"/* XPM */"
'file output #1,"static char *[]={\"4 4 16 1 \","
'file output #1,"\"0 c #000000\",\"1 c #0000BB\",\"2 c #BB0000\",\"3 c #BB00BB\",\"4 c #00BB00\",\"5 c #00BBBB\",\"6 c #BBBB00\",\"7 c #BBBBBB\","
'file output #1,"\"8 c #000000\",\"9 c #0000FF\",\"A c #FF0000\",\"B c #FF00FF\",\"C c #00FF00\",\"D c #00FFFF\",\"E c #FFFF00\",\"F c #FFFFFF\","
'file output #1,"\"0123\",\"4567\",\"89AB\",\"CDEF\"};"
'close #1
'shell("convert _xpm.xpm _gif.gif && rm _xpm.xpm")
shell("convert palette_127c.png _gif.gif && rm fuckyou.xpm")

clstr=0
xed=32:yed=24:wdwd=1
shell("rm _t*.png")
shell("convert "+finp$+" -scale "+str$(xed*4)+"x"+str$(yed*4)+"! _t2.png")
shell("convert "+finp$+" +dither -colors 16 _t4.png")
shell("convert _t4.png +dither -posterize 8 _t5.png")
shell("convert _t5.png +dither -colors 16 _t6.png")

'shell("convert _t2.png -quantize CMY -remap _t6.png -dither Riemersma _t3.png")

shell("convert _t2.png -quantize CMY -remap _gif.gif -dither Riemersma _t3.png")

setdisplay(xed*4,yed*4,32,wdwd):paper(8^8-1):ink(0):pen(0):cls
loadimage("_t3.png",1):pasteicon(0,0,1)
shell("rm _t*.png && rm _gif.gif")

dim p1[16],k1[16]

function grayscale(ct):
  cq=ct
  bq=bitwiseand(cq,255):cq=int(cq/256)
  gq=bitwiseand(cq,255):cq=int(cq/256)
  rq=bitwiseand(cq,255):cq=int(cq/256)
  return int(((rq*30)+(gq*59)+(bq*11))/100)
  end function

for y2=0 to yed-1
  setcaption(str$(int(100*(y2/yed)))+"%")
  for x2=0 to xed-1
    for y1=0 to 3:for x1=0 to 3
      xq=y1*4+x1
      p1[xq]=point(x2*4+x1,y2*4+y1)
      next:next
    for i=0 to 15
      k1[i]=grayscale(p1[i])
      next
    for j=0 to 14:for i=0 to 14
      if k1[i]>k1[i+1] then:
        tm=k1[i]:k1[i]=k1[i+1]:k1[i+1]=tm
        tm=p1[i]:p1[i]=p1[i+1]:p1[i+1]=tm
        end if
      next:next

    if clstr=0 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 6]):dot(x2*4+1,y2*4+0)
    ink(p1[ 9]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[10]):dot(x2*4+1,y2*4+1)
    ink(p1[ 3]):dot(x2*4+2,y2*4+1)
    ink(p1[13]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[ 7]):dot(x2*4+3,y2*4+2)
    ink(p1[ 2]):dot(x2*4+0,y2*4+3)
    ink(p1[12]):dot(x2*4+1,y2*4+3)
    ink(p1[ 5]):dot(x2*4+2,y2*4+3)
    ink(p1[11]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=1 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[ 4]):dot(x2*4+1,y2*4+0)
    ink(p1[ 8]):dot(x2*4+2,y2*4+0)
    ink(p1[12]):dot(x2*4+3,y2*4+0)
    ink(p1[10]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 2]):dot(x2*4+2,y2*4+1)
    ink(p1[ 6]):dot(x2*4+3,y2*4+1)
    ink(p1[ 1]):dot(x2*4+0,y2*4+2)
    ink(p1[ 5]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[11]):dot(x2*4+0,y2*4+3)
    ink(p1[15]):dot(x2*4+1,y2*4+3)
    ink(p1[ 3]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=2 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[10]):dot(x2*4+1,y2*4+0)
    ink(p1[ 1]):dot(x2*4+2,y2*4+0)
    ink(p1[11]):dot(x2*4+3,y2*4+0)
    ink(p1[ 4]):dot(x2*4+0,y2*4+1)
    ink(p1[14]):dot(x2*4+1,y2*4+1)
    ink(p1[ 5]):dot(x2*4+2,y2*4+1)
    ink(p1[15]):dot(x2*4+3,y2*4+1)
    ink(p1[ 8]):dot(x2*4+0,y2*4+2)
    ink(p1[ 2]):dot(x2*4+1,y2*4+2)
    ink(p1[ 9]):dot(x2*4+2,y2*4+2)
    ink(p1[ 3]):dot(x2*4+3,y2*4+2)
    ink(p1[12]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[13]):dot(x2*4+2,y2*4+3)
    ink(p1[ 7]):dot(x2*4+3,y2*4+3)
    end if

    if clstr=3 then:
    ink(p1[ 0]):dot(x2*4+0,y2*4+0)
    ink(p1[12]):dot(x2*4+1,y2*4+0)
    ink(p1[ 3]):dot(x2*4+2,y2*4+0)
    ink(p1[15]):dot(x2*4+3,y2*4+0)
    ink(p1[ 8]):dot(x2*4+0,y2*4+1)
    ink(p1[ 4]):dot(x2*4+1,y2*4+1)
    ink(p1[11]):dot(x2*4+2,y2*4+1)
    ink(p1[ 7]):dot(x2*4+3,y2*4+1)
    ink(p1[ 2]):dot(x2*4+0,y2*4+2)
    ink(p1[14]):dot(x2*4+1,y2*4+2)
    ink(p1[ 1]):dot(x2*4+2,y2*4+2)
    ink(p1[13]):dot(x2*4+3,y2*4+2)
    ink(p1[10]):dot(x2*4+0,y2*4+3)
    ink(p1[ 6]):dot(x2*4+1,y2*4+3)
    ink(p1[ 9]):dot(x2*4+2,y2*4+3)
    ink(p1[ 5]):dot(x2*4+3,y2*4+3)
    end if

    x1=x2 mod 4:y1=y2 mod 4
    u=point(x2*4+x1,y2*4+y1)
    ink(u):dot(x2,y2)
    next:next


open finp$+".bin" for output as #2

for y2=0 to yed-1 step 2
  setcaption(str$(int(100*(y2/yed)))+"%")
  for x2=0 to xed-1
    x2q=x2 mod 2
    c0=point(x2,y2):c1=point(x2,y2+1)

    b0=c0 mod 256:b1=c1 mod 256:c0=int(c0/256):c1=int(c1/256)
    g0=c0 mod 256:g1=c1 mod 256:c0=int(c0/256):c1=int(c1/256)
    r0=c0 mod 256:r1=c1 mod 256

    bv0=0:if b0>120 then:bv0=1:end if
    bv1=0:if b1>120 then:bv1=1:end if
    rv0=0:if r0>120 then:rv0=1:end if
    rv1=0:if r1>120 then:rv1=1:end if
    gv0=0:if g0>120 then:gv0=1:end if
    gv1=0:if g1>120 then:gv1=1:end if

    bq0=abs(b0-128):bq1=abs(b1-128)
    rq0=abs(r0-128):rq1=abs(r1-128)
    gq0=abs(g0-128):gq1=abs(g1-128)

    bh0=0:if bq0>120 then:bh0=1:end if
    bh1=0:if bq1>120 then:bh1=1:end if
    rh0=0:if rq0>120 then:rh0=1:end if
    rh1=0:if rq1>120 then:rh1=1:end if
    gh0=0:if gq0>120 then:gh0=1:end if
    gh1=0:if gq1>120 then:gh1=1:end if

    sh0=bh0+rh0+gh0
    sh1=bh1+rh1+gh1

    attr=bv0+(rv0*2)+(gv0*4)
    attr=attr+(bv1*8)+(rv1*16)+(gv1*32)

    if x2q=0 then
      if sh0>0 then:attr=attr+64:end if
    else
      if sh1>0 then:attr=attr+64:end if
      end if

    writebyte (2,attr)

    next:next

close #2


grab(1,0,0,xed,yed):saveimage("_.bmp",1):shell("convert _.bmp "+finp$+"_"+str$(clstr)+"_.png && rm _.bmp" )

'waitkey

